package com.szjz.elasticsearch.result;

import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.Data;

/**
 * 返回结果模型对象
 */
@Data
public class R<T> {

    private String code = ResultStatusCodeEnum.success.getCode();

    /**
     * 消息
     */
    private String msg;

    /**
     * benma.app.debugMode模式下, 同时返回错误堆栈
     */
    private String stackTrace;

    /**
     * 业务数据
     */
    private Object data;

    private R() {
    }

    public static R build(ResultStatusCodeEnum codeEnum) {
        R r = new R();
        r.code = codeEnum.getCode();
        r.msg = codeEnum.getMsg();
        return r;
    }


    public static R build(Object data, ResultStatusCodeEnum codeEnum) {
        R r = build(codeEnum);
        r.data = data;
        return r;
    }

    public static R build(String msg, ResultStatusCodeEnum codeEnum) {
        R r = build(codeEnum);
        r.msg = msg;
        return r;
    }


    /**
     * 操作成功
     */
    public static R success() {
        return R.build(ResultStatusCodeEnum.success);
    }

    /**
     * 操作成功
     */
    public static R success(Object data) {
        return R.build(data, ResultStatusCodeEnum.success);
    }

    /**
     * 操作失败
     */
    public static R fail() {
        return R.build(ResultStatusCodeEnum.fail);
    }

    /**
     * 数据不存在
     */
    public static R gone() {
        return R.fail("数据不存在");
    }

    /**
     * 操作失败
     */
    public static R fail(String errorMsg) {
        return R.build(errorMsg, ResultStatusCodeEnum.fail);
    }


    /**
     * 未登录
     */
    public static R unauthenticated() {
        return R.build(ResultStatusCodeEnum.unauthenticated);
    }

    /**
     * 未登录
     */
    public static R unauthenticated(String loginUrl) {
        R r = unauthenticated();
        r.setData(loginUrl);
        return r;
    }

    /**
     * 未授权
     */
    public static R forbidden() {
        return R.build(ResultStatusCodeEnum.forbidden);
    }

    /**
     * 账号已禁用
     */
    public static R accountDisabled() {
        return R.build(ResultStatusCodeEnum.account_disabled);
    }

    /**
     * 账号已禁用
     */
    public static R accountDisabled(String errorMsg) {
        return R.build(errorMsg, ResultStatusCodeEnum.account_disabled);
    }

    public static R status(Boolean status) {
        return status ? R.success() : R.fail();
    }


}
